package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportPurchaseOutRpcService;
import com.hunttown.mes.rpc.domain.TransportPurchaseOutDTO;
import com.hunttown.mes.rpc.domain.query.TransportPurchaseOutDTOQuery;
import com.hunttown.mes.service.TransportPurchaseOutService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportPurchaseOutConverts.convert;

/**
 * created by wangjunfu on 2022-03-05 09:16:16
 */
@Service
public class TransportPurchaseOutRpcServiceImpl implements TransportPurchaseOutRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportPurchaseOutRpcServiceImpl.class);

    private final TransportPurchaseOutService transportPurchaseOutService;

    @Autowired
    public TransportPurchaseOutRpcServiceImpl(TransportPurchaseOutService transportPurchaseOutService) {
        this.transportPurchaseOutService = transportPurchaseOutService;
    }

    @Override
    public TransportPurchaseOutDTO insert(TransportPurchaseOutDTO transportPurchaseOutDTO) {
        try {
            return convert(transportPurchaseOutService.insert(convert(transportPurchaseOutDTO)));
        } catch (Exception e) {
            logger.error("TransportPurchaseOut create failed.", e);
            throw new RuntimeException("TransportPurchaseOut create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportPurchaseOutDTO transportPurchaseOutDTO) {
        try {
            return transportPurchaseOutService.updateInfoById(convert(transportPurchaseOutDTO));
        } catch (Exception e) {
            logger.error("TransportPurchaseOut update by id failed.", e);
            throw new RuntimeException("TransportPurchaseOut update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportPurchaseOutDTOQuery transportPurchaseOutDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseOutDTOQuery.getW_id() != null && transportPurchaseOutDTOQuery.getW_id() > 0) {
                query.put("w_id", transportPurchaseOutDTOQuery.getW_id());
            }

            if (transportPurchaseOutDTOQuery.getIds() != null && !transportPurchaseOutDTOQuery.getIds().equals("")) {
                query.put("ids", transportPurchaseOutDTOQuery.getIds());
            }

            if (transportPurchaseOutDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseOutDTOQuery.getStopFlag());
            }

            if (transportPurchaseOutDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseOutDTOQuery.getDeleteFlag());
            }

            return transportPurchaseOutService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportPurchaseOut update by query failed.", e);
            throw new RuntimeException("TransportPurchaseOut update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseOutDTO selectById(Integer id) {
        try {
            return convert(transportPurchaseOutService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportPurchaseOut select by id failed.", e);
            throw new RuntimeException("TransportPurchaseOut select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseOutDTO selectObjByQuery(TransportPurchaseOutDTOQuery transportPurchaseOutDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseOutDTOQuery.getParentId() != null) {
                query.put("parentId", transportPurchaseOutDTOQuery.getParentId());
            }

            if (transportPurchaseOutDTOQuery.getTableId() != null) {
                query.put("tableId", transportPurchaseOutDTOQuery.getTableId());
            }

            if (transportPurchaseOutDTOQuery.getRecordId() != null) {
                query.put("recordId", transportPurchaseOutDTOQuery.getRecordId());
            }

            if (transportPurchaseOutDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseOutDTOQuery.getStopFlag());
            }

            if (transportPurchaseOutDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseOutDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportPurchaseOutDTOQuery.getOrderBy())) {
                query.put("orderBy", transportPurchaseOutDTOQuery.getOrderBy());
            }

            return convert(transportPurchaseOutService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportPurchaseOut select obj by query failed.", e);
            throw new RuntimeException("TransportPurchaseOut select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportPurchaseOutDTO> getListForPage(TransportPurchaseOutDTOQuery transportPurchaseOutDTOQuery) {
        Query query = Query.page(transportPurchaseOutDTOQuery.getPn(), transportPurchaseOutDTOQuery.getPageSize());

        if (transportPurchaseOutDTOQuery.getId() != null && transportPurchaseOutDTOQuery.getId() > 0) {
            query.put("id", transportPurchaseOutDTOQuery.getId());
        }

        if (transportPurchaseOutDTOQuery.getIds() != null && !transportPurchaseOutDTOQuery.getIds().equals("")) {
            query.put("ids", transportPurchaseOutDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportPurchaseOutDTOQuery.getLikeName())) {
            query.put("likeName", transportPurchaseOutDTOQuery.getLikeName());
        }

        if (transportPurchaseOutDTOQuery.getParentId() != null) {
            query.put("parentId", transportPurchaseOutDTOQuery.getParentId());
        }

        if (transportPurchaseOutDTOQuery.getTableId() != null) {
            query.put("tableId", transportPurchaseOutDTOQuery.getTableId());
        }

        if (transportPurchaseOutDTOQuery.getRecordId() != null) {
            query.put("recordId", transportPurchaseOutDTOQuery.getRecordId());
        }

        if (transportPurchaseOutDTOQuery.getPurchaseType() != null) {
            query.put("purchaseType", transportPurchaseOutDTOQuery.getPurchaseType());
        }

        if (transportPurchaseOutDTOQuery.getReceiver() != null && transportPurchaseOutDTOQuery.getReceiver() > 0) {
            query.put("receiver", transportPurchaseOutDTOQuery.getReceiver());
        }

        if (transportPurchaseOutDTOQuery.getHasTable() != null && transportPurchaseOutDTOQuery.getHasTable() > 0) {
            query.put("hasTable", transportPurchaseOutDTOQuery.getHasTable());
        }

        if (StringUtils.isNotBlank(transportPurchaseOutDTOQuery.getShowDate())) {
            query.put("showDate", transportPurchaseOutDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseOutDTOQuery.getStartDate())) {
            query.put("startDate", transportPurchaseOutDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseOutDTOQuery.getEndDate())) {
            query.put("endDate", transportPurchaseOutDTOQuery.getEndDate());
        }

        if (transportPurchaseOutDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportPurchaseOutDTOQuery.getStopFlag());
        }

        if (transportPurchaseOutDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportPurchaseOutDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportPurchaseOutDTOQuery.getOrderBy())) {
            query.put("orderBy", transportPurchaseOutDTOQuery.getOrderBy());
        }

        //区间日期
        if (StringUtils.isNotBlank(transportPurchaseOutDTOQuery.getDaterange())) {
            String[] date = transportPurchaseOutDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        List<TransportPurchaseOutDTO> list = convert(transportPurchaseOutService.selectByQuery(query));
        Page<TransportPurchaseOutDTO> page = Page.from(transportPurchaseOutDTOQuery.getPn(), transportPurchaseOutDTOQuery.getPageSize(), list);
        page.setTotalItems(transportPurchaseOutService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportPurchaseOutService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportPurchaseOut delete failed.", e);
            throw new RuntimeException("TransportPurchaseOut delete failed, Please order your code.", e);
        }
    }
}